# -*- coding: utf-8 -*-
from flask import Flask
from flask import jsonify
from flask import request
import json
import requests
from util.log_handle import LogHandle
from util.string_handle import StringHandle
from util.json_handle import JSONHandle

app = Flask("RobotWebhook", static_folder="static", template_folder="templates")

@app.route("/",methods=["GET", "POST"])
def hello_world():
    return "<p>welcome to the Robot Webhook!</p>"

@app.route("/webhook/event",methods=["GET", "POST"])
def event():
    print(request.headers)
    if request.method == "POST":
        data = request.get_data()
        print(data)
        data = json.loads(data)
        print(data)
        text = data['detail']['content']
        print(text)
        try:
            data = request.get_data()
            print(data)
        except:
            print("request get_data fail")
    return "success"

class SERVER_CONFIG:
    service_host = "0.0.0.0"
    service_port = 2320
    debug_mode = True

    def __init__(self, json_data):
       self.service_host = json_data["service_host"]
       self.service_port = json_data["service_port"]
       self.debug_mode = json_data["debug_mode"]

if __name__ == '__main__':
    configPath = "../config/chat_server_config.json"
    sync_config =  JSONHandle.read_json_file(configPath)
    log = LogHandle(sync_config["log"]["name"], sync_config["log"]["savePath"])
    server_config = SERVER_CONFIG(sync_config)
    app.run(debug = server_config.debug_mode, host = server_config.service_host, port = server_config.service_port)

